{% extends "base.html" %}

{% block content %}
    <div class="card">
        <h2>编辑 {{ table_info.name }} 数据</h2>
        
        <form id="editForm" method="post">
            {# 按照预设的字段顺序显示 #}
            {% for field in table_info.fields %}
                <div class="form-group">
                    <label for="{{ field }}">{{ field }}</label>
                    {% if field in primary_key %}
                        {# 对于复合主键，需要特殊处理 #}
                        {% if table_name == "impart_cards" %}
                            {# impart_cards表的复合主键字段设为只读 #}
                            <input type="text" id="{{ field }}" name="{{ field }}" value="{{ row_data[field] }}" readonly>
                        {% else %}
                            {# 其他表的单主键 #}
                            <input type="text" id="{{ field }}" name="{{ field }}" value="{{ row_data[field] }}" readonly>
                        {% endif %}
                    {% else %}
                        <input type="text" id="{{ field }}" name="{{ field }}" value="{{ row_data[field] }}">
                    {% endif %}
                </div>
            {% endfor %}
            
            <div style="display: flex; gap: 10px; margin-top: 20px;">
                <button type="button" onclick="submitForm('update')" class="btn btn-success">保存</button>
                <button type="button" onclick="confirmDelete()" class="btn btn-danger">删除</button>
                <a href="{{ url_for('table_view', table_name=table_name) }}" class="btn btn-info">返回</a>
            </div>
            
            <input type="hidden" name="action" id="action" value="">
        </form>
    </div>
    
    <script>
        function submitForm(action) {
            document.getElementById('action').value = action;
            
            // 构建表单数据
            const formData = new FormData(document.getElementById('editForm'));
            
            // 对于impart_cards表，需要特殊处理URL
            let url = "{{ url_for('row_edit', table_name=table_name, row_id=request.view_args.row_id) }}";
            
            {% if table_name == "impart_cards" %}
                // 对于复合主键，row_id是user_id和card_name的组合
                // 我们需要确保URL正确
                const user_id = document.getElementById('user_id').value;
                const card_name = document.getElementById('card_name').value;
                const compositeKey = `${user_id}_${card_name}`;
                
                // 更新URL
                url = url.replace('{{ request.view_args.row_id }}', compositeKey);
            {% endif %}
            
            fetch(url, {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert(data.message);
                    window.location.href = "{{ url_for('table_view', table_name=table_name) }}";
                } else {
                    alert('错误: ' + data.error);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('发生错误');
            });
        }
        
        function confirmDelete() {
            if (confirm('确定要删除这条记录吗？此操作不可撤销！')) {
                submitForm('delete');
            }
        }
    </script>
{% endblock %}